package gocache

import (
	"sync"
	"time"
)

// 推荐使用MapCache
// SetWithTTL, ttl>0的时候可能会造成误删除
type SyncMapCache struct {
	lst sync.Map
}

func NewSyncMapCache() CacheOperator {
	return &SyncMapCache{}
}

// SetWithTTL, ttl>0的时候可能会造成误删除
func (this *SyncMapCache) SetWithTTL(k string, v any, ttl time.Duration) {
	this.lst.Store(k, v)
	if ttl > 0 {
		time.AfterFunc(ttl, func() {
			this.Del(k)
		})
	}
}

func (this *SyncMapCache) Del(k string) {
	this.lst.Delete(k)
}

func (this *SyncMapCache) Get(k string) (v any, ok bool) {
	return this.lst.Load(k)
}

func (this *SyncMapCache) Wait() {

}
